package com.tevintao.marketingsystem.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tevintao.marketingsystem.R;
import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.AccountManager;
import com.tevintao.marketingsystem.shell.Shell;
import com.tevintao.marketingsystem.tool.NetWorkTool;
import com.tevintao.marketingsystem.tool.SystemPathTool;

import java.util.ArrayList;

public class ActLocalAccount extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private ListView mList;

    private ArrayList<String> mAccountPhoneNums = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_act_local_account);
        mList = (ListView) findViewById(R.id.act_local_account_list);
        mList.setAdapter(mBaseAdapter);
        mList.setOnItemClickListener(this);
        mList.setOnItemLongClickListener(this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                initData();
            }
        }).start();
    }


    private void initData() {
        mAccountPhoneNums.clear();
        String cmd = "ls " + AccountManager.ACCOUNT_DATA_CACHE_PATH;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        for (String str : command.output) {
//            if (!"clean".equals(str) && !"test".equals(str) && !"net.txt".equals(str) && !"localdev.txt".equals(str)) {
            if (AccountManager.isAccountFile(str)) {
                mAccountPhoneNums.add(str);
            }
            LogManager.getInstance().logInfo(str);
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBaseAdapter.notifyDataSetChanged();
            }
        });
    }

    private BaseAdapter mBaseAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return mAccountPhoneNums.size();
        }

        @Override
        public Object getItem(int position) {
            return mAccountPhoneNums.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = new TextView(ActLocalAccount.this);
            tv.setTextSize(30);
            tv.setText(getItem(position).toString());
            return tv;
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NetWorkTool.setMobileData(this, false);
        final String account = parent.getAdapter().getItem(position).toString();

        new AlertDialog.Builder(this).setTitle("确认切换到帐号  " + account)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switchAccount(account);
                    }
                }).setNegativeButton("返回", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).show();

    }

    private void switchAccount(final String account) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String createAccount = AccountManager.getInstance(ActLocalAccount.this).switchContext(account);
                LogManager.getInstance().logInfo("切换帐号 " + createAccount);
                if (AccountManager.SUCCESS_TAG.equals(createAccount)) {
                    mList.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ActLocalAccount.this, "切换帐号成功", Toast.LENGTH_LONG).show();
//                            Toast.makeText(ActLocalAccount.this, "切换帐号成功  切换通讯录" + (changeContacts ? "成功" : "失败"), Toast.LENGTH_LONG).show();
                        }
                    });

                    NetWorkTool.setMobileData(ActLocalAccount.this, true);
                } else {
                    mList.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ActLocalAccount.this, "切换帐号失败", Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        final String account = parent.getAdapter().getItem(position).toString();

        new AlertDialog.Builder(this).setTitle("确认删除初帐号  " + account)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteAccount(account);
                    }
                }).setNegativeButton("返回", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).show();


        return true;
    }

    private void deleteAccount(final String account) {
        new AlertDialog.Builder(this).setTitle("确认要删除本地帐号吗？")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                String accountPath = AccountManager.ACCOUNT_DATA_CACHE_PATH + account;
                                String sdAccountPath = AccountManager.SD_MS_ROOT + "/" + account;
                                if (SystemPathTool.exitFile(accountPath)) {
                                    final boolean remove = SystemPathTool.remove(accountPath);
                                    final boolean removeSD = SystemPathTool.remove(sdAccountPath);
                                    mList.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(ActLocalAccount.this, "删除帐号 " + account + (remove ? "成功" : "失败") + " 删除SD卡帐号 " + account + (removeSD ? "成功" : "失败"), Toast.LENGTH_LONG).show();

                                        }
                                    });
                                    if (remove)
                                        initData();
                                }
                            }
                        }).start();
                    }
                }).setNegativeButton("返回", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).show();
    }
}
